package cn.schoolwow.quickdao.domain.provider;

import cn.schoolwow.quickdao.domain.database.parent.DatabaseType;

public class H2DatabaseProvider implements DatabaseProvider {
    @Override
    public String name() {
        return "h2";
    }

    @Override
    public DatabaseType getDatabaseType() {
        return DatabaseType.H2;
    }

    @Override
    public String comment(String comment) {
        return "";
    }

    @Override
    public String escape(String value) {
        return "`" + value.toUpperCase() + "`";
    }

    @Override
    public boolean returnGeneratedKeys() {
        return true;
    }

    @Override
    public String convertJavaTypeToColumnType(String javaType) {
        switch (javaType){
            case "byte":
            case "java.lang.Byte":{return "TINYINT";}
            case "[B":{return "BINARY";}
            case "boolean":
            case "java.lang.Boolean":{return "BOOLEAN";}
            case "char":{return "CHAR";}
            case "java.lang.Character":{return "CHARACTER";}
            case "short":
            case "java.lang.Short":{return "SMALLINT";}
            case "int":{return "INT";}
            case "java.lang.Integer":{return "INTEGER";}
            case "float":
            case "java.lang.Float":{return "REAL";}
            case "long":
            case "java.lang.Long":{return "BIGINT";}
            case "double":
            case "java.lang.Double":{return "DOUBLE";}
            case "java.lang.String":{return "VARCHAR(255)";}
            case "java.util.Date":{return "DATETIME";}
            case "java.sql.Date":{return "DATE";}
            case "java.sql.Time":{return "TIME";}
            case "java.sql.Timestamp":{return "TIMESTAMP";}
            case "java.time.LocalDate":{return "DATE";}
            case "java.time.LocalDateTime":{return "DATETIME";}
            case "java.sql.Array":{return "ARRAY";}
            case "java.math.BigDecimal":{return "DECIMAL";}
            case "java.sql.Blob":{return "BLOB";}
            case "java.sql.Clob":{return "CLOB";}
            case "java.sql.NClob":{return "NCLOB";}
            case "java.io.InputStream":
            case "java.io.Reader":{return "LONGTEXT";}
        }
        throw new IllegalArgumentException("目前不支持转换java数据类型["+javaType+"]为数据库类型");
    }

    @Override
    public String convertColumnTypeToJavaType(String columnType) {
        String rawColumnType = columnType;
        if(columnType.contains("(")){
            columnType = columnType.substring(0, columnType.indexOf("("));
        }
        columnType = columnType.trim().toUpperCase();
        switch (columnType){
            case "TINYINT":{return "Byte";}
            case "SMALLINT":{return "Short";}
            case "INT":
            case "INTEGER":{return "Integer";}
            case "BIGINT":{return "Long";}
            case "REAL":{return "Float";}
            case "DOUBLE":{return "Double";}
            case "BINARY":{return "[B";}
            case "BOOLEAN":{return "Boolean";}
            case "CHAR":
            case "CHARACTER":{return "Character";}
            case "VARCHAR":{return "String";}
            case "DATETIME":{return "java.time.LocalDateTime";}
            case "DATE":{return "java.time.LocalDate";}
            case "TIME":{return "java.sql.Time";}
            case "TIMESTAMP":{return "java.sql.Timestamp";}
            case "ARRAY":{return "java.sql.Array";}
            case "DECIMAL":{return "java.math.BigDecimal";}
            case "BLOB":{return "java.sql.Blob";}
            case "CLOB":{return "java.sql.Clob";}
            case "NCLOB":{return "java.sql.NClob";}
            case "LONGTEXT":{return "java.io.InputStream";}
        }
        throw new IllegalArgumentException("目前不支持转换数据库类型["+rawColumnType+"]为java数据类型");
    }

}
